<?php
	/**
	 * @author		: Mauricio Amorim
	 * @date		: 01/01/2010
	 * @version		: 1.0
	 * @tabela		: contact
	 * @content		: 	
	 * 
			`id`, 
			`active`, 
			`subject`, 
			`name`, 
			`company`, 
			`email`, 
			`phone`, 
			`message`, 
			`date`

	 *
	 * @obs			:
	 */
//importa classes de apoio
include_once "library/".Config::FOLDER_APPLICATION."/modules/contact/dao/ContactDAO.class.php";
include_once "library/facil3/core/vo/BaseVO.class.php";
include_once "library/facil3/interface/vo/PatternVO.class.php";

class ContactVO extends BaseVO implements PatternVO{
	
	public $subject;
	public $name;
	public $company;
	public $email; 
	public $phone; 
	public $message; 	 
	public $date;
  	
  	function __construct($arrayFetchPost = NULL){
		//inicia funo de validacao
		parent::_construct();
		$this->__table = "contact";
		//se enviar $arrayFetchPost é para distrinchar internamente os dados do usuario
		if($arrayFetchPost !== NULL){
			//enviou array para distrinchar
			$this->setFetchArray($arrayFetchPost);
		}
	}//end construct
	/**
	 * inicia internamente a DAO
	 * @return void
	 */
	public function startDAO(){
		$this->DAO = ContactDAO::getInstance();
	}
	/**
	 * precisa enviar uma array dentro dos padr�es dessa VO, pode enviar diretamente o $_POST
	 * @param (array) $array_dados
	 * @return void
	 */
	function setFetchArray($array_dados){
		//$this->id = DataHandler::getValueByArrayIndex($array_dados, "id");
		//$this->active = DataHandler::getValueByArrayIndex($array_dados, "active");
		$this->name = DataHandler::getValueByArrayIndex($array_dados, 		"name");
		$this->subject = DataHandler::getValueByArrayIndex($array_dados, 	"subject");
		$this->company = DataHandler::getValueByArrayIndex($array_dados, 	"company");
		$this->email = DataHandler::getValueByArrayIndex($array_dados, 		"email");
		$this->phone = DataHandler::getValueByArrayIndex($array_dados, 		"phone");
		$this->message = DataHandler::getValueByArrayIndex($array_dados, 	"message");
	}
	/**
	 * @param (object) $obj_dados
	 * @return void
	 */
	function setFetchObject($obj_dados){
		$this->id = DataHandler::getValueByStdObjectIndex($obj_dados, "id");
		$this->active = DataHandler::getValueByStdObjectIndex($obj_dados, "active");
		$this->name = DataHandler::getValueByStdObjectIndex($obj_dados, "name");
		$this->subject = DataHandler::getValueByStdObjectIndex($obj_dados, "subject");
		$this->company = DataHandler::getValueByStdObjectIndex($obj_dados, "company");
		$this->email = DataHandler::getValueByStdObjectIndex($obj_dados, "email");
		$this->phone = DataHandler::getValueByStdObjectIndex($obj_dados, "phone");
		$this->message = DataHandler::getValueByStdObjectIndex($obj_dados, "message");
	}
	

	//---------------------------------------------------------  SETs
	
	/**
	 * @param int active
	 */
	//public function setActive($active){
	//	if($active !== NULL){
	//		$this->active = DataHandler::forceInt($active);
	//	}
	//}
	/**
	 * @param $name
	 */
	public function setName($name){
		$this->name = DataHandler::forceString($name);
	}
	/**
	 * @param subject
	 */
	public function setSubject($subject){
		$this->subject = DataHandler::forceString($subject);
	}
	/**
	 * @param company
	 */
	public function setCompany($company){
		$this->company = DataHandler::forceString($company);
	}
	/**
	 * @param $email
	 */
	public function setEmail($email){
		$this->email = DataHandler::forceString($email);
	}
	/**
	 * @param $phone
	 */
	public function setPhone($phone){
		$this->phone = DataHandler::forceString($phone);
	}
	/**
	 * @param $message
	 */
	public function setMessage($message){
		$this->message = DataHandler::forceString($message);
	}
	/**
	 * @param date (datetime)
	 */
	public function setDate($date){
		if($date !== NULL){
			$this->date = DataHandler::convertDateToDB($date);
		}
	}
	
	// ------------------------------------------------------   GETs
	/**
	 * @return str
	 */
	public function getName(){
		return $this->name;
	}
	/**
	 * @return str
	 */
	public function getSubject(){
		return $this->subject;
	}
	/**
	 * @return str
	 */
	public function getCompany(){
		return $this->company;
	}
	/**
	 * @return str
	 */
	public function getEmail(){
		return $this->email;
	}
	/**
	 * @return str
	 */
	public function getPhone(){
		return $this->phone;
	}
	/**
	 * @return str
	 */
	public function getMessage(){
		return $this->message;
	}
	
	/**
	 * @param $locale
	 * @return date (datetime)
	 */
	public function getDate($locale = NULL){
		return DataHandler::convertDate($locale, $this->date);
	}
	
	/**
	 * Validação da VO para ver se está apta a ser cadastrada
	 * @return array
	 */
	function validate(){
		//vai validar a VO inteira conforme as regras
		$ReturnResultVO = new ReturnResultVO();
		$ReturnResultVO->success = TRUE;
		if(!Validation::validateEmail($this->getEmail())){
			$ReturnResultVO->success = FALSE;
			$ReturnResultVO->addMessage(LibraryLanguage::ERROR_CONTACT_INVALID_EMAIL." (".$this->getEmail().")");
		}
		if(!Validation::blank($this->getName(), 2)){
			$ReturnResultVO->success = FALSE;
			$ReturnResultVO->addMessage(LibraryLanguage::ERROR_CONTACT_INVALID_NAME."(".$this->getName().")");
		}
		if(!Validation::blank($this->getMessage(), 2)){
			$ReturnResultVO->success = FALSE;
			$ReturnResultVO->addMessage(LibraryLanguage::ERROR_CONTACT_INVALID_MESSAGE."(".$this->getMessage().")");
		}
		return $ReturnResultVO;
	}
	
	/**
	 * @param int $tempResult
	 * @param array $arrayReturn
	 */
	public function resultHandler($tempResult, &$arrayReturn){
		switch($tempResult){
			case DbInterface::ERROR_UPDATE_DONOT_HAVE_ID:
				$arrayReturn[] = LibraryLanguage::ERROR_CONTACT_UPDATE_DONOT_HAVE_ID;
				break;
			case DbInterface::ERROR_UPDATE_DONOT_HAVE_UPDATES:
				$arrayReturn[] = LibraryLanguage::ERROR_CONTACT_UPDATE_DONOT_HAVE_UPDATE;
				break;
			case DbInterface::ERROR_UPDATE_COMMIT:
				$arrayReturn[] = LibraryLanguage::ERROR_CONTACT_UPDATE_COMMIT;
				break;
			case DbInterface::ERROR_INSERT_COMMIT:
				$arrayReturn[] = LibraryLanguage::ERROR_CONTACT_INSERT_COMMIT;
				break;
			case DbInterface::SUCCESS:
				$arrayReturn[] = LibraryLanguage::SUCCESS_CONTACT;
				break;
			default:
				$arrayReturn[] = "erro sinistro($tempResult).";
				break;
		}//end switch
	}
	
}